#ifndef __SAFE_POINTER_H__
  #define __SAFE_POINTER_H__

#include "exceptions.h"
#include "global_variables.h"

using namespace std;

//! Safe pointer class

/*!
This class eliminates the need for checking every single pointer by throwing an exception instead of crashing.
The object which is being pointed to, will never be deleted by this class, for such a functionality please
use smart_pointer class.

Implementation date: 2009
*/

template<class T>
class safe_pointer {
private:
  T *pointer;

public:
  //! returns true if the content is not set yet or if it is null
  bool is_null() const {
    return (pointer == 0);
  } // is_null

  //! (may throw an exception)
  T *operator->() const {
    if (pointer != 0)
      return pointer;
    else
      throw pointer_exception(global_empty_string);
  }

  //! returns the content of the safe_pointer (may throw an exception)
  T &operator*() const {
    if (pointer == 0)
      throw pointer_exception(global_empty_string);

    return *pointer;
  }

  //! default constructor
  safe_pointer() : pointer(0) {
    // nothing to do
  }

  //! constructor
  safe_pointer(T *pointer) : pointer(pointer) {
    // nothing to do
  }

  //! destructor (non-virtual: please do not inherit from this class)
  ~safe_pointer() {
    pointer = 0; // safe_pointer does not delete the pointer, for such a functionality use the smart_pointer
  }
};

#endif
